<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>

        <el-form-item label="赠品名称" prop="giftName">
          <el-input v-model="queryParams.giftName" placeholder="请输入赠品名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="领取人名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入领取人名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <!-- <el-form-item label="领取时间" prop="createTime">
          <el-date-picker clearable size="small" v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择领取时间">
          </el-date-picker>
        </el-form-item> -->

          <el-form-item label="领取时间" prop="createTimeArray">
            <el-date-picker v-model="queryParams.createTimeArray" type="daterange" align="right" clearable
              value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>

      </el-row>
      <el-row>
        <el-form-item label="是否使用" prop="isUsed">
          <el-select v-model="queryParams.isUsed" placeholder="请选择是否使用" clearable size="small">
            <el-option v-for="dict in isUsedOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="数据状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择数据状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 20px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-row>

    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['activity:detail:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:detail:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:detail:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['activity:detail:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<div class="divTable">
    <el-table border v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="赠品名称" align="center" prop="giftName" :show-overflow-tooltip="true" />
      <el-table-column label="领取人名称" align="center" prop="customerName" :show-overflow-tooltip="true" />
      <el-table-column label="领取人头像" align="center" prop="customerUrlOne" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.customerUrlOne != null" placement="right" title="" trigger="click">
            <el-image slot="reference" :src="scope.row.customerUrlOne" :alt="scope.row.customerUrlOne"
              style="max-height: 45px;max-width: 45px"></el-image>
            <el-image :src="scope.row.customerUrlOne"></el-image>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="是否使用" align="center" prop="isUsed" :formatter="isUsedFormat" sortable />
      <el-table-column label="使用时间" align="center" prop="usedTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usedTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="createTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="failureTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.failureTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据状态" align="center" prop="dataStatus" :formatter="dataStatusFormat" sortable />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['activity:detail:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:detail:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['activity:detail:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
</div>
    <!-- 添加或修改用户赠品领取信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="领取人ID" prop="customerUid">
          <el-input v-model="form.customerUid" placeholder="请输入领取人ID" />
        </el-form-item>
        <el-form-item label="领取人名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入领取人名称" />
        </el-form-item>
        <el-form-item label="领取人头像" prop="customerUrl">
          <el-input v-model="form.customerUrl" placeholder="请输入领取人头像" />
        </el-form-item>
        <el-form-item label="赠品ID" prop="giftUid">
          <el-input v-model="form.giftUid" placeholder="请输入赠品ID" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderUid">
          <el-input v-model="form.orderUid" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="数据状态" prop="dataStatus">
          <el-select v-model="form.dataStatus" placeholder="请选择数据状态">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否使用" prop="isUsed">
          <el-select v-model="form.isUsed" placeholder="请选择是否使用">
            <el-option v-for="dict in isUsedOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用时间" prop="usedTime">
          <el-date-picker clearable size="small" v-model="form.usedTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效时间" prop="failureTime">
          <el-date-picker clearable size="small" v-model="form.failureTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择失效时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看用户赠品领取信息对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDetail,
    getDetail,
    delDetail,
    addDetail,
    updateDetail
  } from "@/api/activity/giftDetail";

  export default {
    name: "Detail",
    components: {},
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户赠品领取信息表格数据
        detailList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 数据状态字典
        dataStatusOptions: [],
        // 是否使用字典
        isUsedOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          giftName: null,
          customerName: null,
          dataStatus: null,
          isUsed: null,
          usedTime: null,
          createTime: null,
          failureTime: null,
          createTimeArray: null
        },
        columns: [{
            label: 'ID',
            dataIndex: 'detailUid',
          },
          {
            label: '领取人ID',
            dataIndex: 'customerUid',
          },
          {
            label: '领取人名称',
            dataIndex: 'customerName',
          },
          {
            label: '领取人头像',
            dataIndex: 'customerUrl',
          },
          {
            label: '赠品ID',
            dataIndex: 'giftUid',
          },
          {
            label: '赠品ID',
            dataIndex: 'giftName',
          },
          {
            label: '订单ID',
            dataIndex: 'orderUid',
          },
          {
            label: '数据状态',
            dataIndex: 'dataStatus',
          },
          {
            label: '是否使用',
            dataIndex: 'isUsed',
          },
          {
            label: '使用时间',
            dataIndex: 'usedTime',
          },
          {
            label: '领取时间',
            dataIndex: 'createTime',
          },
          {
            label: '失效时间',
            dataIndex: 'failureTime',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.dataStatusOptions = response.data;
      });
      this.getDicts("activity_coupon_use").then(response => {
        this.isUsedOptions = response.data;
      });
    },
    methods: {
      /** 查询用户赠品领取信息列表 */
      getList() {
        this.loading = true;
        listDetail(this.queryParams).then(response => {
          this.detailList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 数据状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 是否使用字典翻译
      isUsedFormat(row, column) {
        return this.selectDictLabel(this.isUsedOptions, row.isUsed);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          detailUid: null,
          customerUid: null,
          customerName: null,
          customerUrl: null,
          giftUid: null,
          giftName: null,
          orderUid: null,
          dataStatus: null,
          isUsed: null,
          usedTime: null,
          createTime: null,
          failureTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.createTimeArray = null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.detailUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户赠品领取信息";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看用户赠品领取信息";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const detailUid = row.detailUid || this.ids
        getDetail(detailUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改用户赠品领取信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.detailUid != null) {
              updateDetail(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDetail(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const detailUids = row.detailUid || this.ids;
        this.$confirm('是否确认删除用户赠品领取信息编号为"' + detailUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(detailUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('activity/giftDetail/export', {
          ...this.queryParams
        }, `activity_detail.xlsx`)
      }
    }
  };
</script>
